package com.ctsi.security.manager.impl;


import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.cache.CommUtilCache;
import com.ctsi.commons.util.response.Page;
import com.ctsi.organise.common.api.OrganiseFactoryBean;
import com.ctsi.organise.common.api.OrganiseServiceProxy;
import com.ctsi.organise.common.model.Organise;
import com.ctsi.organise.common.model.UserLogin;
import com.ctsi.security.FetchObjIds;
import com.ctsi.security.domain.DataObject;
import com.ctsi.security.exception.ParamNotValideException;

import java.util.*;

public class OrganFetchObjIds implements FetchObjIds {
	private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(OrganFetchObjIds.class);
	
	private OrganiseServiceProxy organiseService = OrganiseFactoryBean.getInstance().getOrganiseProxy();
	private String key;

	// 生成缓存
	private static final CommUtilCache<String, Set<String>> cacheUserLoginDataIds = new CommUtilCache<String, Set<String>>("user.doTypeAction");

	/**
	 * 对于这种类型来说，实际上，会忽略action，只是根据组织判断
	 */
	public Set<String> getDataObjectIdsByCache(final String userLoginId,final  String doTypeId, final String action) {
		Set<String> result = cacheUserLoginDataIds.get(userLoginId);
		if (result != null) {
			return result;
		}
		result =Collections.unmodifiableSet( getDataObjectIds(userLoginId, doTypeId, action));
		// 放入缓存
		cacheUserLoginDataIds.put(userLoginId,result);
		return result;
	}

	/**
	 * 由登录用户的Id找到相应的人员Id
	 * 
	 * @param userLoginId 登陆用户id,不可变
	 * @return
	 */
	private String getPartyIdFromLoginId(String userLoginId) {
		if (UtilValidate.isEmpty(userLoginId)) {
			throw new ParamNotValideException("登录用户参数不能为空！");
		}
		UserLogin userLogin = OrganiseFactoryBean.getInstance().getUserLoginProxy().findById(userLoginId);
		if (userLogin == null) {
			logger.error("user :{} no UserLogin",userLoginId);
			throw new ParamNotValideException("登录用户参数不合法，请检查！id:"+userLoginId);
		}
		String partyId = userLogin.getPartyId();
		return partyId;
	}

	public void setOrganiseService(OrganiseServiceProxy organiseService) {
		this.organiseService = organiseService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 */
	// 得到dataObjectId到dataObject的映射
	public Map<String, DataObject> getDataObject(String userLoginId, String doType, String action) {
		Set<String> ids = getDataObjectIdsByCache(userLoginId, doType, action);
		Map<String, DataObject> result = new HashMap<String, DataObject>();
		if (UtilValidate.isNotEmpty(ids)) {
			for (String id : ids) {
				Organise organise = organiseService.findOrganiseById(id);
				if (organise != null) {
					DataObject item = new DataObject();
					item.setDataObjectId(id);
					item.setDataObjectName(organise.getPartyName());
					item.setDescription("独立组织");
					item.setDataObjectTypeId("organCorp");
					item.setId(UUID.randomUUID().toString());
					result.put(id, item);
				}
			}
		}
		return result;
	}

	public Set<String> getDataObjectIds(final String userLoginId, String doTypeId, String action) {
		Set<String> result = new HashSet<String>();
		// 得到当前登录用户对应的独立组织
		String partyId = getPartyIdFromLoginId(userLoginId);
		// 当前登录用户对应的人员Id
		Organise organise = organiseService.getPartyCorp(partyId);
		if (organise != null) {
			result.add(organise.getPartyId());
		}else{
			logger.info("party id {} ,no Corporate organization",partyId);
		}
		return  result;
	}

	// 清除缓存
	public static void removeCache(String userLoginId) {
		if (userLoginId == null) {
			cacheUserLoginDataIds.clear();
		} else {
			cacheUserLoginDataIds.remove(userLoginId);
		}
	}

	public boolean isUserRole() {
		return false;
	}

	public List<DataObject> queryDataObject(String typeId, String parent) {
		return null;
	}

	public Page<DataObject> queryDataObjectPage(String typeId, Map<String, Object> param, int start, int pageSize) {
		return null;
	}

	public DataObject findDataObjectById(String id, String typeId) {
		return null;
	}

	public String  getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

}
